package com.gjintel.framework.paytype.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.ModelAndView;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.log.model.LogInfo;
import com.gjintel.framework.log.util.LogUtil;
import com.gjintel.framework.log.util.LogUtil.LogType;
import com.gjintel.framework.paytype.model.PayType;
import com.gjintel.framework.paytype.service.IPayTypeService;
import com.gjintel.framework.user.model.User;
import com.gjintel.framework.util.CommUtil;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.util.StringUtil;

@RequestMapping("/business/paytype")
@Controller
public class PayTypeController {

	private static final Logger LOG = Logger
			.getLogger(PayTypeController.class);

	@Autowired
	private IPayTypeService payTypeService;


	/**
	 * @description 获得输入数据信息
	 * @author fengya
	 * @date 2016-3-15 下午03:55:05
	 * @param id
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping("/input")
	public ModelAndView input(String payid) {
		ModelAndView mv = new ModelAndView(
				"/business/paytype/paytype-input");
		if (!StringUtil.isEmpty(payid)) {
			PayType payType = payTypeService.get(PayType.class, payid);
			mv.addObject("payType", payType);
		}
		return mv;
	}

	/**
	 * @description 获得的所有的记录数据
	 * @author fengya
	 * @date 2016-3-15 下午03:55:22
	 * @return
	 * @return JSONArray
	 */
	@RequestMapping("/listAll")
	@ResponseBody
	public JSONObject listAll() {
		List<PayType> list = this.payTypeService.findAll();
		return JSONObject.fromObject(list);
	}


	/**
	 * @description 分页查询数据
	 * @author fengya
	 * @date 2016-3-15 下午03:55:40
	 * @param page
	 * @param payType
	 * @param sort
	 * @return
	 * @return JSONObject
	 */
	@RequestMapping("/list")
	@SuppressWarnings("unchecked")
	@ResponseBody
	public JSONObject list(Page page, PayType payType) {
		this.payTypeService.findAll(page, payType);
		return CommUtil.getPageJSONData(page, true);
	}

	/**
	 * @description 保存数据
	 * @author fengya
	 * @date 2016-3-15 下午03:55:52
	 * @param session
	 * @param payType
	 * @return
	 * @return String
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(HttpSession session, PayType payType) {
		User user = (User) session.getAttribute("loginUser");
		if (payType != null) {
			boolean sign = false;
			if (StringUtil.isEmpty(payType.getId())) {
				payType.setId(null);
				payType.setCreater(user.getName());
				payType.setCreateTime(new Date());
				sign = true;
				List<PayType> payTypes=this.payTypeService.findAll();
				for (PayType payyType : payTypes) {
					//方式类别名称不能相同
					if (payyType.getName().equals(payType.getName())) {
						return Constant.SAVE_UN_SUCCESS;
					}
				}
			}
			payType.setUpdate_time(new Date());
			this.payTypeService.save(payType);
			if (sign) {
				LogUtil
						.addLog(new LogInfo("添加交易方式", user.getId(), user
								.getName(), String.valueOf(LogType.ADD
								.ordinal()), payType.getClass()
								.getSimpleName(), payType.getId()));
			} else {
				LogUtil
						.addLog(new LogInfo("更新交易方式", user.getId(), user
								.getName(), String.valueOf(LogType.UPDATE
								.ordinal()), payType.getClass()
								.getSimpleName(), payType.getId()));
				payType.setUpdate_time(new Date());

			}
			return Constant.SAVE_SUCCESS;
		}
		return Constant.SAVE_UN_SUCCESS;
	}

	/**
	 * @description 删除数据
	 * @author fengya
	 * @date 2016-3-15 下午03:56:08
	 * @param deleteIds
	 * @return
	 * @throws Exception
	 * @return String
	 */
	@RequestMapping("/remove")
	@ResponseBody
	public String remove(HttpSession session, String deleteIds)
			throws Exception {
		this.payTypeService.removeData(deleteIds);
		User user = (User) session.getAttribute("loginUser");
		LogUtil.addLog(new LogInfo("删除交易方式", user.getId(), user.getName(),
				String.valueOf(LogType.DELETE.ordinal()), PayType.class
						.getSimpleName(), deleteIds));
		WebApplicationContext webApplicationContext = ContextLoader
				.getCurrentWebApplicationContext();
		ServletContext servletContext = webApplicationContext
				.getServletContext();
		return Constant.DEL_SUCCESS;
	}

}
